package controller

import (
	"GoAdmin/common"
	"GoAdmin/models"
	"github.com/gin-gonic/gin"
	"github.com/go-xorm/xorm"
	"log"
	"net/http"
	"strconv"
	"time"
	"xorm.io/builder"
)

func CarInformation(c *gin.Context) {
	var (
		number   string
		schoolId string
	)
	page, _ := strconv.Atoi(c.DefaultPostForm("page", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultPostForm("pageSize", "15"))
	offset := (page - 1) * pageSize
	user, _ := common.GetCtxUser(c)
	filter := builder.NewCond()
	if user.Type == "0" || user.Type == "1" {
		//管理用户查询全部
	} else {
		if user.Province != "" {
			filter = filter.And(builder.Eq{"sl.province": user.Province})
		}
		if user.City != "" {
			filter = filter.And(builder.Eq{"sl.city": user.City})
		}
		if user.District != "" {
			filter = filter.And(builder.Eq{"sl.district": user.District})
		}
	}
	if user.Type == "3" {
		//同一个地区双代理
		filter = filter.And(builder.Eq{"sl.config": user.SchoolList})
	}
	if schoolId = c.PostForm("school_id"); schoolId != "" {
		filter = filter.And(builder.Eq{"car.school_id": schoolId})
	}
	if number = c.PostForm("number"); number != "" {
		filter = filter.And(builder.Like{"car.plate_number", number})
	}
	filter = filter.And(builder.Eq{"car.isused": "1"})
	count, list, _ := new(models.Car).GetCarByFilter(filter, pageSize, offset)
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "查询错误", "data": gin.H{"list": list, "count": count}})
	return
}

func CarDetail(c *gin.Context) {
	var (
		coachId string
	)
	log.Println(c.PostForm("coach_id"))
	if coachId = c.PostForm("coach_id"); coachId == "" {
		c.JSON(http.StatusOK, gin.H{"code": 201, "msg": "查询错误", "data": nil})
		return
	}
	has, coachDetail, _ := new(models.Coach).GetCoachDetailInfo(coachId)
	if !has {
		c.JSON(http.StatusOK, gin.H{"code": 201, "msg": "查询错误", "data": nil})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "查询成功", "data": coachDetail})
	return
}

func CarRecordList(c *gin.Context) {
	var (
		carId string
	)
	page, _ := strconv.Atoi(c.DefaultPostForm("pageIndex", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultPostForm("pageSize", "15"))
	offset := (page - 1) * pageSize
	log.Println(c.PostForm("car_id"))
	if carId = c.PostForm("car_id"); carId == "" {
		c.JSON(http.StatusOK, gin.H{"code": 201, "msg": "查询错误", "data": nil})
		return
	}
	carRecord, count, _ := new(models.LearnRecord).GetCarRecordList(carId, pageSize, offset)

	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "查询成功", "data": gin.H{"list": carRecord, "count": count}})
	return
}

func CarAdd(c *gin.Context) {
	var (
		err    error
		school models.School
	)
	if c.PostForm("school_id") == "" {
		c.JSON(http.StatusOK, gin.H{"code": 201, "msg": "驾校信息异常"})
		return
	}
	if school, _ = new(models.School).GetSchool(c.PostForm("school_id")); school.Id == 0 {
		c.JSON(http.StatusOK, gin.H{"code": 201, "msg": "驾校信息异常"})
		return
	}
	//if school.RecordStatus == "0" {
	//	Resp(c, 201, "该驾校未备案", nil)
	//	return
	//}
	car := new(models.Car)
	portrait := new(models.CarPortrait)
	if err = c.ShouldBind(car); err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 201, "msg": err.Error()})
		return
	}
	if err = c.ShouldBind(portrait); err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 201, "msg": err.Error()})
		return
	}
	if car.PlateNumber != "" && car.HasPlateNumber(1) {
		c.JSON(http.StatusOK, gin.H{"code": 201, "msg": "车辆已存在"})
		return
	}
	if err = c.ShouldBind(portrait); err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 201, "msg": "请上传车辆照片"})
		return
	}
	var engine *xorm.Engine
	engine = models.Engine
	session := engine.NewSession()
	defer session.Close()
	// add Begin() before any action
	err = session.Begin()
	if c.PostForm("purchase_date") == "" {
		car.PurchaseDate = ""
	} else {
		car.PurchaseDate = c.PostForm("purchase_date")
	}
	if c.PostForm("on_service_date") == "" {
		car.OnServiceDate = time.Now()
	} else {
		car.OnServiceDate, _ = time.Parse("2006-01-02", c.PostForm("on_service_date"))
	}
	if c.PostForm("out_service_date") == "" {
		car.OnServiceDate = time.Now()
	} else {
		car.OutServiceDate, _ = time.Parse("2006-01-02", c.PostForm("out_service_date"))
	}
	if c.PostForm("energytype") != "" {
		car.EnergyType = c.PostForm("energytype")
	}
	car.Isused = "1"
	car.HisplateNumber = ""
	car.RecordStatus = "0"
	car.ApprovalStatus = "0"
	car.Remark = ""

	_, err = session.Table("dts_car").Insert(car)
	if err != nil {
		session.Rollback()
		c.JSON(http.StatusOK, gin.H{"code": 201, "msg": err.Error()})
		return
	}
	portrait.CarId = car.Id
	portrait.AddTime = time.Now()
	_, err = session.Table("dts_car_portrait").Insert(portrait)
	if err != nil {
		session.Rollback()
		c.JSON(http.StatusOK, gin.H{"code": 201, "msg": err.Error()})
		return
	}
	err = session.Commit()
	if err != nil {
		session.Rollback()
		c.JSON(http.StatusOK, gin.H{"code": 201, "msg": err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "添加车辆成功"})
	return

}
